package com.yunbao.project.basedata.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunbao.core.respones.ResultEnum;
import com.yunbao.project.basedata.query.StorageQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunbao.core.respones.Result;
import com.yunbao.core.respones.ResultBuilder;
import com.yunbao.project.basedata.entity.Storage;
import com.yunbao.project.basedata.service.IStorageService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 仓库 前端控制器
 * </p>
 *
 * @author 安能
 * @since 2019-09-16
 */
@RestController
@RequestMapping("storage")
public class StorageController {


    @Resource
    IStorageService storageService;


    /**
     * 增加仓库
     *
     * @param storage {@link Storage }
     * @return Result
     */
    @PostMapping("insert")
    public Result insert(@RequestBody Storage storage) {

        storageService.save(storage);
        return ResultBuilder.success(ResultEnum.ADD_SUCCESS);
    }

    /**
     * 根据id删除仓库
     *
     * @param id id
     * @return Result
     */
    @PostMapping("deleteById")
    public Result deleteById(Integer id) {

        storageService.removeById(id);
        return ResultBuilder.success(ResultEnum.DELETE_SUCCESS);
    }

    /**
     * 根据id修改仓库
     *
     * @param storage storage
     * @return Result
     */
    @PostMapping("updateById")
    public Result updateById(@RequestBody Storage storage) {

        storageService.updateById(storage);
        return ResultBuilder.success(ResultEnum.UPDATA_SUCCESS);
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return 仓库信息
     */
    @GetMapping("getById")
    @Transactional()
    public Result getById(Integer id) throws Exception {

        Storage storage = storageService.getById(id);
        return ResultBuilder.success(storage);
    }


    /**
     * 获取仓库列表查询
     *
     * @param storageQuery {@link StorageQuery 编号,姓名,联系人,电话 }
     * @return 仓库列表
     */
    @PostMapping("getList")
    public Result getLists(@RequestBody StorageQuery storageQuery) {
        IPage<Storage> page = storageService.page(new Page<>(
                        storageQuery.getCurrent(),
                        storageQuery.getSize()
                )
        );
        return ResultBuilder.success(page);
    }

    /**
     * 获取仓库列表加模糊查询
     *
     * @param storageQuery {@link StorageQuery 编号,姓名,联系人,电话 }
     * @return 仓库列表
     */
    @PostMapping("getListLike")
    public Result getListLike(@RequestBody StorageQuery storageQuery) {

        IPage<Storage> listLike = storageService.getListLike(storageQuery);
        return ResultBuilder.success(listLike);
    }


}
